package com.beerandjesus.sportofkings.handicapper.factory.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformance;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformanceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PastPerformanceFactoryImpl extends AbstractObjectFactoryImpl implements PastPerformanceFactory {

    private static final int FIRST_INDEX = 256;
    private static final int MAX_RECORDS = 10;

    /* (non-Javadoc)
     * @see com.beerandjesus.sportofkings.handicapper.factory.brisnet.singlefile.PastPerformanceFactory#getPastPerformances(java.lang.String)
     */
    public List<PastPerformance> getPastPerformances(String line) {
        String[] values = getValues(line);
        return getPastPerformances(values);
    }

    /* (non-Javadoc)
     * @see com.beerandjesus.sportofkings.handicapper.factory.brisnet.singlefile.PastPerformanceFactory#getPastPerformances(java.lang.String[])
     */
    public List<PastPerformance> getPastPerformances(String[] values) {
        List<PastPerformance> list = new ArrayList<PastPerformance>();
        int idx;
        for (int i = 0; i < MAX_RECORDS; i++) {

            PastPerformanceImpl pp = new PastPerformanceImpl();
            idx = FIRST_INDEX + i;

            String date = val(values, idx);

            if (StringUtils.isEmpty(date.trim())) {
                break;
            }
            pp.setDate(date);
            // Only 9 of these, days since previous race for 10th race may not be available
            if (i < 10) {
                pp.setNumDaysSincePrevious(val(values, idx = idx + MAX_RECORDS));
            } else {
                idx = idx + MAX_RECORDS;
                pp.setNumDaysSincePrevious("");
            }
            pp.setTrack(val(values, (idx = idx + MAX_RECORDS)));
            pp.setBrisTrackCode(val(values, (idx = idx + MAX_RECORDS)));
            pp.setRaceNumber(val(values, (idx = idx + MAX_RECORDS)));
            pp.setTrackCondition(val(values, (idx = idx + MAX_RECORDS)));
            pp.setDistanceYards(val(values, (idx = idx + MAX_RECORDS)));
            pp.setSurface(u.getTrackSurface(val(values, (idx = idx + MAX_RECORDS))));
            pp.setSpecialChuteIndicator(val(values, (idx = idx + MAX_RECORDS)));
            pp.setNumEntrants(val(values, (idx = idx + MAX_RECORDS)));
            pp.setPostPosition(val(values, (idx = idx + MAX_RECORDS)));
            pp.setEquipment(val(values, (idx = idx + MAX_RECORDS)));
            pp.setRaceNameOfPrevRaces(val(values, (idx = idx + MAX_RECORDS)));
            pp.setMedication(u.getMedication(val(values, (idx = idx + MAX_RECORDS))));
            pp.setTripComment(val(values, (idx = idx + MAX_RECORDS)));
            pp.setWinnerName(val(values, (idx = idx + MAX_RECORDS)));
            pp.setPlaceFinisherName(val(values, (idx = idx + MAX_RECORDS)));
            pp.setShowFinisherName(val(values, (idx = idx + MAX_RECORDS)));
            pp.setWinnerWeightCarried(val(values, (idx = idx + MAX_RECORDS)));
            pp.setPlacerWeightCarried(val(values, (idx = idx + MAX_RECORDS)));
            pp.setShowerWeightCarried(val(values, (idx = idx + MAX_RECORDS)));
            pp.setWinnerMargin(val(values, (idx = idx + MAX_RECORDS)));
            pp.setPlacerMargin(val(values, (idx = idx + MAX_RECORDS)));
            pp.setShowerMargin(val(values, (idx = idx + MAX_RECORDS)));
            pp.setExtraComment(val(values, (idx = idx + MAX_RECORDS)));
            pp.setWeight(val(values, (idx = idx + MAX_RECORDS)));
            pp.setOdds(val(values, (idx = idx + MAX_RECORDS)));
            pp.setEntryFlag(val(values, (idx = idx + MAX_RECORDS)));
            pp.setRaceClassification(val(values, (idx = idx + MAX_RECORDS)));
            pp.setClaimingPrice(val(values, (idx = idx + MAX_RECORDS)));
            pp.setPurse(val(values, (idx = idx + MAX_RECORDS)));
            pp.setCallPositionStart(val(values, (idx = idx + MAX_RECORDS)));
            pp.setCallPositionFirst(val(values, (idx = idx + MAX_RECORDS)));
            pp.setCallPositionSecond(val(values, (idx = idx + MAX_RECORDS)));
            pp.setCallPositionGate(val(values, (idx = idx + MAX_RECORDS)));
            pp.setCallPositionStretch(val(values, (idx = idx + MAX_RECORDS)));
            pp.setFinishPosition(val(values, (idx = idx + MAX_RECORDS)));
            pp.setMoneyPosition(val(values, (idx = idx + MAX_RECORDS)));

            pp.setCallStartLenOrMargin(val(values, idx = idx + MAX_RECORDS));
            pp.setCallStartBtwnLenthsOnly(val(values, idx = idx + MAX_RECORDS));
            pp.setCall1stLenOrMargin(val(values, idx = idx + MAX_RECORDS));
            pp.setCall1stBtwnLengthsOnly(val(values, idx = idx + MAX_RECORDS));
            pp.setCall2ndLenOrMargin(val(values, idx = idx + MAX_RECORDS));
            pp.setCall2ndBtwnLengthsOnly(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisRaceShape1stCall(val(values, idx = idx + MAX_RECORDS));
            //706-715 reserved
            idx = idx + MAX_RECORDS;
            pp.setCallStrLenOrMargin(val(values, idx = idx + MAX_RECORDS));
            pp.setCallStrBtwnLengthsOnly(val(values, idx = idx + MAX_RECORDS));
            pp.setFinishLenOrMargin(val(values, idx = idx + MAX_RECORDS));
            pp.setFinishBtwnLengthsOnly(val(values, idx = idx + MAX_RECORDS));

            pp.setBrisRaceShape2ndCall(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisPaceFigure2f(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisPaceFigure4f(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisPaceFigure6f(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisPaceFigure8f(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisPaceFigure10f(val(values, idx = idx + MAX_RECORDS));
            pp.setBrisPaceFigureLate(val(values, idx = idx + MAX_RECORDS));
            // 826-835 Reserved
            idx = idx + MAX_RECORDS;
            // 836 = 845 Reserved
            idx = idx + MAX_RECORDS;
            pp.setBrisSpeedRating(val(values, idx = idx + MAX_RECORDS));
            pp.setSpeedRating(val(values, idx = idx + MAX_RECORDS));

            pp.setTrackVariant(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction2f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction3f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction4f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction5f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction6f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction7f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction8f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction10f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction12f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction14f(val(values, idx = idx + MAX_RECORDS));
            pp.setFraction16f(val(values, idx = idx + MAX_RECORDS));

            pp.setFractionNumOne(val(values, idx = idx + MAX_RECORDS));
            pp.setFractionNumTwo(val(values, idx = idx + MAX_RECORDS));
            pp.setFractionNumThree(val(values, idx = idx + MAX_RECORDS));
            // 1016-1025 Reserved
            idx = idx + MAX_RECORDS;
            //1026-1035 Reserved
            idx = idx + MAX_RECORDS;
            pp.setFinalTime(val(values, idx = idx + MAX_RECORDS));
            pp.setClaimedCode(val(values, idx = idx + MAX_RECORDS));
            pp.setTrainerName(val(values, idx = idx + MAX_RECORDS));
            pp.setJockeyName(val(values, idx = idx + MAX_RECORDS));
            pp.setApprenticeWtAllowance(val(values, idx = idx + MAX_RECORDS));
            pp.setRaceType(u.getRaceType(val(values, idx = idx + MAX_RECORDS)));
            pp.setAgeSexRestrictions(val(values, idx = idx + MAX_RECORDS));
            pp.setStateBredFlag(val(values, idx = idx + MAX_RECORDS));
            pp.setRestrQualifFlag(val(values, idx = idx + MAX_RECORDS));
            pp.setFavoriteFlag(val(values, idx = idx + MAX_RECORDS));
            pp.setFrontBandagesFlag(val(values, idx + MAX_RECORDS));

            list.add(pp);
        }

        return list;
    }

}
